<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <script src="../vue.js"></script>
    </head>

    <body>
        <div id="app">
            <button @click="type=1">升序</button>
            <button @click="type=2">降序</button>
            <button @click="type=3">默认</button>
            <input type="text" v-model="search" />
            <ul>
                <li v-for="person in newPersons" :key="person.id">
                    姓名：{{person.name}}---年龄：{{person.age}}
                </li>
            </ul>
            <input type="text" v-model="search" />
            <ul>
                <li v-for="person in newPersonsHandlder" :key="person.id">
                    姓名：{{person.name}}---年龄：{{person.age}}
                </li>
            </ul>
        </div>
        <script>
            Vue.config.productionTip = false;

            const vm = new Vue({
                el: "#app",
                data() {
                    return {
                        type: 3,
                        search: "",
                        persons: [
                            { id: "001", name: "周杰伦", age: 19 },
                            { id: "002", name: "陈奕迅", age: 25 },
                            { id: "003", name: "周杰", age: 39 },
                            { id: "004", name: "陈冠希", age: 7 },
                            { id: "005", name: "谢坤", age: 22 },
                            { id: "006", name: "杨坤", age: 14 },
                            { id: "007", name: "谢霆锋", age: 11 },
                        ],
                        newPersons: [],
                    };
                },
                computed: {
                    newPersonsHandlder() {
                        const fina = this.persons.filter((item) => {
                            return item.name.includes(this.search);
                        });
                        switch (this.type) {
                            case 1: {
                                return fina.sort((a, b) => {
                                    return b.age - a.age;
                                });
                            }
                            case 2: {
                                return fina.sort((a, b) => {
                                    return a.age - b.age;
                                });
                            }
                            case 3: {
                                return fina;
                            }
                        }
                    },
                },
                watch: {
                    type: {
                        immediate: true,
                        handler() {},
                    },
                    search: {
                        immediate: true,
                        handler() {
                            this.newPersons = this.persons.filter((item) => {
                                return item.name.includes(this.search);
                            });
                        },
                    },
                },
            });
        </script>
    </body>
</html>
